import { _decorator, instantiate, Node, NodePool, Prefab } from 'cc';
import { ConfigKey, EventName, level_monster_Refresh_config, monster_Refresh_config } from '../../manager/AppControll';
import { DataHelper } from '../../system/DataHelper';
import { Loader } from '../../tools/LoadManger';
import { MonsterNormal } from '../monster/MonsterNormal';
import { Boss } from '../monster/Boss';
import EvtManger from '../../tools/EvtManger';
import { MonsterManager } from '../monster/MonsterManager';
const { ccclass, property } = _decorator;

@ccclass('LevelMonsterManager')
export class LevelMonsterManager {
    private refreshConfig: level_monster_Refresh_config = null;
    private bIsResh = false;
    private nCurTime = 0;
    private nInterval = 0;
    private ndMonsterParent: Node = null;
    private curLevelCount = 0;
    private isStart = false;

    static ndBoss: Node = null;
    static ltMonster: Node[] = [];
    static hitPrefab: Prefab = null;

    constructor(node: Node) {
        this.ndMonsterParent = node;
        Loader.LoadPrefab("hit", (prefab) => {
            LevelMonsterManager.hitPrefab = prefab;
        })
    }

    StartLevel(config: level_monster_Refresh_config) {
        this.curLevelCount = 0;
        this.refreshConfig = config;
        this.isStart = true;
    }

    Tick(dt) {
        if (!this.isStart) return;
        this.nInterval += dt;
        if (this.nInterval >= this.refreshConfig.refreshInterval && this.curLevelCount < this.refreshConfig.maxMonster) {
            this.nInterval = 0;
            //根据怪物判定是否出现的是BOSS
            this.CreateMonster();
            this.curLevelCount++;
        }
    }

    CreateMonster() {
        let id = this.GetMonsterID();
        if (id == Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.bossId).value)) {
            this.CreateBoss();
            return;
        }
        let monsterConfig = DataHelper.GetMonsterBaseConfigById(id);
        if (!monsterConfig) return;
        if (!this.refreshConfig) return;
        let node = MonsterManager.normalMonsterPool.get();
        if (node) {
            LevelMonsterManager.ltMonster.push(node);
            this.ndMonsterParent.addChild(node);
            node.getComponent(MonsterNormal).InitMonster(this.refreshConfig as monster_Refresh_config, monsterConfig);
        }

        if (LevelMonsterManager.ndBoss != null) {
            LevelMonsterManager.ndBoss.setSiblingIndex(10000);
        }
    }

    CreateBoss() {
        EvtManger.do(EventName.levelGift);
        let monsterConfig = DataHelper.GetMonsterBaseConfigById(100017);
        Loader.LoadPrefab("temporaryBoss", (prefab) => {
            if (LevelMonsterManager.ndBoss != null) {
                return;
            }
            let boss = instantiate(prefab);
            this.ndMonsterParent.addChild(boss);
            boss.addComponent(Boss).InitMonster(this.refreshConfig as monster_Refresh_config, monsterConfig);
            LevelMonsterManager.ndBoss = boss;
            LevelMonsterManager.ltMonster.push(boss);
            LevelMonsterManager.ndBoss.setSiblingIndex(10000);
        })
    }

    GetMonsterID(): number {
        let pool = this.refreshConfig.monsterPool;
        let totalWeight = 0;
        let weightSum = 0;
        pool.forEach((element) => {
            totalWeight += element[1];
        })
        let randomNumber = Math.random();
        for (let i = 0; i < pool.length; i++) {
            weightSum += pool[i][1];
            if (weightSum >= randomNumber * totalWeight) {
                return pool[i][0];
            }
        }
    }

    ChangeLevelMode() {
        this.ndMonsterParent.destroyAllChildren();
        LevelMonsterManager.ltMonster = [];
        LevelMonsterManager.ndBoss = null;
    }
}


